/***********************************************************************
 * nScreen - An Advanced Console Application Framework for .NET
 * 
 * Copyright (C) 2007 by:
 *  Justin Long (dukk(a)dukk.org)
 *  Fabio Cavalcante (fabio(a)codesapien.com)
 *	Josh Perry (josh(a)6bit.com)
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as 
 * published by  the Free Software Foundation; either version 2 of the 
 * License or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this package (See COPYING.LIB); if not, write to the 
 * Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330
 * Boston, MA 02111-1307, USA.
 ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using nScreen.Controls;

namespace nScreen
{
	public abstract class Screen : ScreenControl
	{
		private string _title = string.Empty;
		private int _width = 80;
		private int _height = 25;

		public event EventHandler KeyPress;

		public string Title
		{
			get { return Console.Title; }
			set { Console.Title = value; }
		}

		public int Width
		{
			get { return _width; }
			set
			{
				_width = value;
				 
				//Console.BufferWidth = this.Width;
				Console.WindowWidth = this.Width;
			}
		}

		public int Height
		{
			get { return _height; }
			set
			{
				_height = value;

				//Console.BufferHeight = this.Height;
				Console.WindowHeight = this.Height;
			}
		}

		public virtual void OnKeyPress(ConsoleKeyInfo keyInfo)
		{
			EventHandler handler = this.KeyPress;

			if (null != handler)
			{
				handler(this, EventArgs.Empty);
			}
		}

		public override void Invalidate()
		{
			if (!ScreenApplication.RenderSuspended)
			{
				this.OnRender();
			}
		}

		protected override void OnRender()
		{
			if (!ScreenApplication.RenderSuspended)
			{
				Console.Clear();

				base.OnRender();

				Console.SetCursorPosition(0, 0);
				Console.ResetColor();
			}
		}
	}
}